﻿using EasyCore.App;
using Microsoft.AspNetCore.Http;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EasyAdmin.Web.Middlewares
{
    /// <summary>
    /// 全局设置的中间件：主要设置当前请求相关的内容
    /// </summary>
    public class ApplicationMiddleware
    {
        private readonly RequestDelegate _next;
        private readonly IHttpContextAccessor _httpContextAccessor;
        public ApplicationMiddleware(RequestDelegate next, IHttpContextAccessor httpContextAccessor)
        {        
            this._next = next;
        }

        public async Task Invoke(HttpContext context)
        {
            //获得当前请求的相关信息
            if (EasyApp.HttpContextAccessor == null) EasyApp.HttpContextAccessor = new AsyncLocal<IHttpContextAccessor>();
            EasyApp.HttpContextAccessor.Value = _httpContextAccessor;
            await _next(context);
        }
    }
}
